package com.googlecode.beanfiles;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Indicates a non-fatal problem with a translation.
 */
public class TranslationNotice implements Comparable<TranslationNotice> {

    protected static Log log() {
        return LogFactory.getLog(TranslationNotice.class);
    }

    private int row;
    private Object value;
    private String propertyName;
    private int column;
    private String message;

    public TranslationNotice(String message) {
        this.message = message;
    }
    
    public TranslationNotice(String message, int column) {
        this.message = message;
        this.column = column;
    }
    
    public TranslationNotice(String message, int column, Object value) {
        this.message = message;
        this.column = column;
        this.value = value;
    }

    public TranslationNotice(int column) {
        this.column = column;
    }

    public TranslationNotice(int row, Object value) {
        this.row = row;
        this.value = value;
    }

    public TranslationNotice(int row, Object value, String propertyName) {
        this.row = row;
        this.value = value;
        this.propertyName = propertyName;
    }

    public TranslationNotice(Object value, String propertyName) {
        this.value = value;
        this.propertyName = propertyName;
    }

    public int getRow() {
        return row;
    }

    public void setRow(int row) {
        this.row = row;
    }

    public Object getValue() {
        return value;
    }

    public void setValue(Object value) {
        this.value = value;
    }

    public String getPropertyName() {
        return propertyName;
    }

    public void setPropertyName(String propertyName) {
        this.propertyName = propertyName;
    }

    public int getColumn() {
        return column;
    }

    public void setColumn(int column) {
        this.column = column;
    }

    public String toString() {
        if (message != null) {
            return message;
        }
        if (value == null || value.toString().trim().equals("null")) {
            return new StringBuilder("Empty value found at row '").append(getRow()).append("', column '").append(getColumn()).append(" (")
                .append(getSpreadSheetColumnName(column)).append(")'").toString();
        } else {
            return new StringBuilder("Value of '").append(getValue()).append("' found at row '").append(getRow()).append("', column '")
                .append(getColumn()).append(" (").append(getSpreadSheetColumnName(column)).append(")'").toString();
        }

    }

    public int compareTo(TranslationNotice o) {
        // return low rows first
        int comparison = o.getRow() - getRow();
        if (comparison != 0) {
            return comparison;
        }

        comparison = o.getColumn() - getColumn();
        if ((o.getColumn() != 0 || getColumn() != 0) && comparison != 0) {
            return comparison;
        }

        if (propertyName != null) {
            return propertyName.compareTo(o.getPropertyName());
        }

        return 0;
    }

    private String[] letters =
            { "", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X",
                "Y", "Z" };

    public String getSpreadSheetColumnName(int column) {
        String letters = this.letters[column % 26];
        while ((column = column - 26) > 0) {
            letters += this.letters[column % 26];
        }
        return letters;
    }

    public boolean equals(Object o) {
        if (! (o instanceof TranslationNotice)) {
            return false;
        } else {
            TranslationNotice other = (TranslationNotice) o;
            
            if (propertyName != null && !propertyName.equals(other.getPropertyName())) {
                return false;
            } else {
                return column == other.getColumn() && row == other.getRow();
            }
        }
    }
    
}
